// pages/me/sign/sign.js
const app = getApp()
import {getUserSign, forumSignin} from '../../../api/api.js';
import { tips } from '../../../utils/show.js';
import { formatDate } from '../../../utils/util.js'

Page({
  data: {
    height: app.globalData.height * 2 + 40,
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '签到  ', //导航栏 中间的标题
      img: "../../image/back.png"
    }, 
    disabled:false, //按钮禁用
    avatar: '',
    nickname: '',
    score: 0,
    count: 0,
    imgUrl: app.globalData.imgUrl
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 判断用户是否登陆
    if (!app.globalData.hasLogin){
      // console.log('跳转到授权')
    } else {
      this.setData({
        nickname: app.globalData.userInfo.nickname,
        avatar : app.globalData.userInfo.avatar
      })
      this.getList();
    }
    
  },
  getList(){
    let date = new Date().getTime();
    date = new Date(date);
    date = formatDate(date);
    const id = app.globalData.userId
    getUserSign({ id }).then(res => {
      if (res.code === 0) {
        let data = res.data;
        if (date == res.data.update_time){
          this.setData({ disabled: true });
        }
        const score = parseInt(data.total_point) - parseInt(data.used_point);
        this.setData({
          avatar: app.globalData.userInfo.avatar,
          nickname: app.globalData.userInfo.nickname,
          score: score,
          count: data.count,
        })
      } else {
        tips("数据出错");
      }
    });
  },
  //点击事件
  ckSign(e){
    this.setData({
      disabled:true
    });
    app.showLoading();
    let v = {
      openid: app.globalData.openid
    };
    forumSignin(v).then(res =>{
      if (res.code === 0) {
        const score = parseInt(res.data.total_point) - parseInt(res.data.used_point);
        this.setData({
          count:res.data.count,
          score
        });
        app.globalData.userInfo.total_point = res.data.total_point;
        app.globalData.userInfo.used_point = res.data.used_point;
        app.hideLoading();
        wx.showToast({
          title: '签到成功',
          con: 'none',
          duration: 2000
        });
      }else{
        app.hideLoading();
        tips("签到成功")
      }
    })
  }
})